package net.betou.cms.dao.assist.impl;

import java.util.List;

import net.betou.cms.dao.assist.CmsCommentDao;
import net.betou.cms.entity.assist.CmsComment;
import net.betou.common.hibernate3.Finder;
import net.betou.common.hibernate3.HibernateBaseDao;
import net.betou.common.page.Pagination;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;


@Repository
public class CmsCommentDaoImpl extends HibernateBaseDao<CmsComment, Integer> implements CmsCommentDao {
	
	public Pagination getPage(Integer siteId, Integer contentId,
			Integer greaterThen, Boolean checked, boolean recommend,
			boolean desc, int pageNo, int pageSize, boolean cacheable) {
		Finder f = getFinder(siteId, contentId, null,null,greaterThen, checked,
				recommend, desc, cacheable);
		return find(f, pageNo, pageSize);
	}

	@SuppressWarnings("unchecked")
	public List<CmsComment> getList(Integer siteId, Integer contentId,
			Integer greaterThen, Boolean checked, boolean recommend,
			boolean desc, int count, boolean cacheable) {
		Finder f = getFinder(siteId, contentId, null,null,greaterThen, checked,
				recommend, desc, cacheable);
		f.setMaxResults(count);
		return find(f);
	}
	public Pagination getPageForMember(Integer siteId, Integer contentId,Integer toUserId,Integer fromUserId,
			Integer greaterThen, Boolean checked, Boolean recommend,
			boolean desc, int pageNo, int pageSize,boolean cacheable){
		Finder f = getFinder(siteId, contentId, toUserId,fromUserId,greaterThen, checked,
				recommend, desc, cacheable);
		return find(f, pageNo, pageSize);
	}
	public List<CmsComment> getListForDel(Integer siteId, Integer userId,
			Integer commentUserId, String ip){
		Finder f = Finder.create("from CmsComment bean where 1=1");
		if (siteId != null) {
			f.append(" and bean.site.id=:siteId");
			f.setParam("siteId", siteId);
		}
		if(commentUserId!=null){
			f.append(" and bean.commentUser.id=:commentUserId");
			f.setParam("commentUserId", commentUserId);
		}
		if(userId!=null){
			f.append(" and bean.content.user.id=:fromUserId");
			f.setParam("fromUserId", userId);
		}
		f.setCacheable(false);
		return find(f);
	}

	private Finder getFinder(Integer siteId, Integer contentId,Integer toUserId,Integer fromUserId,
			Integer greaterThen, Boolean checked, Boolean recommend,
			boolean desc, boolean cacheable) {
		Finder f = Finder.create("from CmsComment bean where 1=1");
		if (contentId != null) {
			f.append(" and bean.content.id=:contentId");
			f.setParam("contentId", contentId);
		} else if (siteId != null) {
			f.append(" and bean.site.id=:siteId");
			f.setParam("siteId", siteId);
		}
		if(toUserId!=null){
			f.append(" and bean.commentUser.id=:commentUserId");
			f.setParam("commentUserId", toUserId);
		}else if(fromUserId!=null){
			f.append(" and bean.content.user.id=:fromUserId");
			f.setParam("fromUserId", fromUserId);
		}
		if (greaterThen != null) {
			f.append(" and bean.ups>=:greatTo");
			f.setParam("greatTo", greaterThen);
		}
		if (checked != null) {
			f.append(" and bean.checked=:checked");
			f.setParam("checked", checked);
		}
		if(recommend!=null){
			if (recommend) {
				f.append(" and bean.recommend=true");
			}
		}
		if (desc) {
			f.append(" order by bean.id desc");
		} else {
			f.append(" order by bean.id asc");
		}
		f.setCacheable(cacheable);
		return f;
	}

	public CmsComment findById(Integer id) {
		CmsComment entity = get(id);
		return entity;
	}

	public CmsComment save(CmsComment bean) {
		getSession().save(bean);
		return bean;
	}

	public CmsComment deleteById(Integer id) {
		CmsComment entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	public int deleteByContentId(Integer contentId) {
		String hql = "delete from CmsComment bean where bean.content.id=:contentId";
		return getSession().createQuery(hql).setParameter("contentId",
				contentId).executeUpdate();
	}
	
	@Override
	protected Class<CmsComment> getEntityClass() {
		return CmsComment.class;
	}

	
	
	// =========================================== 
	public Pagination getPage( String queryContentTitle, String queryCommentIp,
			Boolean checked, Boolean recommend, Integer siteId, Integer order,
			Boolean cacheable, Short ups, int pageNo, int pageSize ) {
		Finder f = this.getFinder( queryContentTitle, queryCommentIp, checked, recommend, siteId, order, cacheable, ups );
		return find(f, pageNo, pageSize);
	}
	
	private Finder getFinder( String queryContentTitle, String queryCommentIp, Boolean checked, Boolean recommend, Integer siteId, Integer order,
												Boolean cacheable, Short ups){
		
		Finder f = Finder.create( "from CmsComment bean where 1=1" );
		
		if( StringUtils.isNotBlank( queryContentTitle )){
			f.append( " and bean.content.contentExt.title like :contentTitle" );
			f.setParam( "contentTitle", "%" + queryContentTitle + "%" );
		
		}
		
	    if (siteId != null) {
			f.append(" and bean.site.id=:siteId");
			f.setParam("siteId", siteId);
		}
	    
	    if( StringUtils.isNotBlank( queryCommentIp )){
	    	f.append( " and bean.commentExt.ip =:ip" );
			f.setParam( "ip", queryCommentIp );
	    }
	    
	    if (checked != null ) {
			f.append(" and bean.checked=:checked");
			f.setParam("checked", checked);
		}
		if(recommend!=null){
			if (recommend) {
				f.append(" and bean.recommend=true");
			}
		}
		
		if (ups != null && ups > 0 ) {
			f.append(" and bean.ups>=:greatTo");
			f.setParam("greatTo", ups);
		}
		
		f.append( getOrder( order ) );
	
		f.setCacheable(cacheable);
		
		return f;
		
	}
	
	
	private String getOrder( Integer order ){
		
		if( order == null || order == 0){
			return " order by bean.id desc ";
		}
		String orderHql;
		
		switch( order ){
			case 1:
				orderHql = " order by bean.createTime desc ";
				break;
			case 2:
				orderHql =  " order by bean.createTime asc";
				break;
			case 3:
				orderHql =  " order by bean.content.contentExt.title desc ";
				break;
			case 4:
				orderHql =  " order by bean.content.contentExt.title asc ";
				break;
			case 5:
				orderHql =  " order by bean.ups desc ";
				break;
			default:
				orderHql = " order by bean.id desc ";
				break;
		}
		return orderHql;
	}
	
	
}